import { createRouter, createWebHistory } from 'vue-router'
// import { useUserStore } from '@/stores/user'
// import { ElMessage } from 'element-plus'
import Home from '@/views/Home.vue'
import Login from '@/views/Login.vue'
import UserInfo from '@/views/components/UserInfo.vue'
import productInfo from '@/views/components/supplierManagement/productInfo.vue'
import Layout from '@/views/components/common/Layout.vue'
import productType from '@/views/components/supplierManagement/productType.vue'
import WorkorderList from '@/views/components/workOrderManage/workorderList.vue'
import OrderDetail from '@/views/components/workOrderManage/orderDetail.vue'
import UserList from '@/views/components/userInfoManage/UserList.vue'
import UserInfoDetail from '@/views/components/userInfoManage/userInfoDetail.vue'
import SelfWorkOrder from '@/views/components/workOrderManage/selfWorkOrder.vue'
import CustomerWorkorder from '@/views/components/workOrderManage/customerWorkorder.vue'
import SupplierInfo from '@/views/components/supplierManagement/supplierInfo.vue'
import PermissionUserList from '@/views/components/permissionManage/permissionUserList.vue'
import RoleList from '@/views/components/permissionManage/roleList.vue'
import PermissionList from '@/views/components/permissionManage/permissionList.vue'
import LayoutUser from '@/views/components/common/LayoutUser.vue'
import UserHome from '@/views/components/UserView/UserHome.vue'
import PersonalInfo from '@/views/components/UserView/personalInfo.vue'
import UserWorkorder from '@/views/components/UserView/userWorkorder.vue'
import ArchivedWorkorder from '@/views/components/workOrderManage/archivedWorkorder.vue'
import CompanyUser from '@/views/components/userInfoManage/companyUser.vue'
import NormalUser from '@/views/components/userInfoManage/normalUser.vue'
import EmployeeInfo from '@/views/components/employeeManage/employeeInfo.vue'
import MessageView from '@/views/components/messageView.vue'
const routes = [
  // 1. 登录页路由（独立，不包含Layout）
  // {
  //   path: '/',
  //   redirect: '/login'
  // },

  {
    path: '/',           // 根路径就是登录页
    name: 'Login',
    component: Login
  },
  {
    path: '/login',
    redirect: '/'        // 输入 /login 也回到根路径，保持唯一入口
  },
  {
    path:'/orderDetail/:id',
    component:OrderDetail
  },
  {
    path:'/userInfoDetail/:id',
    component:UserInfoDetail
  },
  // 2. 主布局路由（包含侧边栏、轮播等公共部分）
  {
    path: '/home',
    component: Layout,
    meta: { roles: ['ADMIN'] },
    children: [
      { path: '/home', component: Home }, // 首页：/home
      { path: 'userInfo', component: UserInfo }, // 用户信息：/userInfo
      { path: 'productInfo', component: productInfo }, // 设备列表：/equipment
      {path:'productType',component:productType},
      {path:'workorderList',component:WorkorderList},
      {path:'userList',component:UserList},
      {path:'companyUser',component:CompanyUser},
      {path:'normalUser',component:NormalUser},
      {path:'selfWorkOrder',component:SelfWorkOrder},
      {path:'customerWorkorder',component:CustomerWorkorder},
      {path:'supplierInfo',component:SupplierInfo},
      {path:'permissionUserList',component:PermissionUserList},
      {path:'roleList',component:RoleList},
      {path:'permissionList',component:PermissionList},
      {path:'archivedWorkorder',component:ArchivedWorkorder},
      {path:'employeeInfo',component:EmployeeInfo},
      {path:'messageView',component:MessageView}
    ]
  },
  {
    path:'/user',
    component:LayoutUser,
    meta: { roles: ['DISPATCHER'] },
    children:[
      {path:'/userHome',component:UserHome},
      {path:'/userWorkorder',component:UserWorkorder},
      {path:'/personalInfo',component:PersonalInfo}
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫：未登录时跳转到登录页（可选）
router.beforeEach((to, from, next) => {
  const isLogin = !!localStorage.getItem('token')

  if (to.path === '/') {
    next()
  } else {
    isLogin ? next() : next('/')
  }
})
export default router
